/**
 * 文件上传js
 */

$(function () {
	initialPage();
	getGrid();
});

function initialPage() {
	$(window).resize(function() {
		$('#dataGrid').bootstrapTable('resetView', {height: $(window).height()-56});
	});
}

function getGrid() {
	$('#dataGrid').bootstrapTableEx({
		url: '../../sys/oss/list?_' + $.now(),
		height: $(window).height()-56,
		queryParams: function(params){
			params.keyword = vm.keyword;
			return params;
		},
		columns: [
			{checkbox: true},
			{field : "url", title : "URL地址", width : "100px"}, 
			{field : "createDate", title : "创建时间", width : "100px"}
		]
	})

    new AjaxUpload('#upload', {
        action: '../../sys/oss/upload?token=' + token,
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if(vm.config.type == null){
                alert("云存储配置未配置");
                return false;
            }
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
            dialogLoading(true);
        },
        onComplete : function(file, r){
            dialogLoading(false);
            if(r.code == 0){
                dialogMsg(r.msg, 'success');
                vm.load();
            }else{
                alert(r.msg);
            }
        }
    });

}

var vm = new Vue({
	el:'#dpLTE',
	data: {
		keyword: {
		    key:null
        },
        config:{},
        title:"云存储配置",
    },
    created: function(){
        this.getConfig();
    },
	methods : {
        load:function () {
            $('#dataGrid').bootstrapTable('refresh');
        },
        getConfig: function () {
            $.getJSON("../../sys/oss/config", function(r){
                vm.config = r.config;
            });
        },
		load: function() {
			$('#dataGrid').bootstrapTable('refresh');
		},
		save: function() {
			dialogOpen({
				title: '新增文件上传',
				url: 'sys_html/oss/add.html?_' + $.now(),
                width: '80%',
                height: '80%',
				yes : function(iframeId) {
					top.frames[iframeId].vm.acceptClick();
				},
			});
		},
		edit: function() {
			var ck = $('#dataGrid').bootstrapTable('getSelections');
			if(checkedRow(ck)){
				dialogOpen({
					title: '编辑文件上传',
					url: 'sys_html/oss/edit.html?_' + $.now(),
                    width: '80%',
                    height: '80%',
					success: function(iframeId){
						top.frames[iframeId].vm.sysOss.id = ck[0].id;
						top.frames[iframeId].vm.setForm();
					},
					yes: function(iframeId){
						top.frames[iframeId].vm.acceptClick();
					}
				});
			}
		},
		remove: function() {
			var ck = $('#dataGrid').bootstrapTable('getSelections'), ids = [];	
			if(checkedArray(ck)){
				$.each(ck, function(idx, item){
					ids[idx] = item.id;
				});
				$.RemoveForm({
					url: '../../sys/oss/remove?_' + $.now(),
			    	param: ids,
			    	success: function(data) {
			    		vm.load();
			    	}
				});
			}
		},
        //云配置弹窗
        openCloudConfigDlg: function() {
            dialogOpen({
                title: '云存储配置',
                url: 'sys_html/oss/cloudConfig.html?_' + $.now(),
                width: '80%',
                height: '60%',
                yes : function(iframeId) {
                    top.frames[iframeId].vm.acceptClick();
                },
            });
        },
	}
})